package com.synjones.ui.recognize;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.Pair;

import com.synjones.Constant;
import com.synjones.entry.SignPerson;
import com.synjones.face.camera.FaceHelper;
import com.synjones.idcard.IDCard;
import com.synjones.network.Api;
import com.synjones.ui.others.OthersFragment;
import com.synjones.idcardusb_reader.IdCardHelper;

import java.util.List;

public class RecognizeSecPresenter implements RecognizeContract.Presenter {


    private RecognizeContract.View view;
    private IdCardHelper idCardHelper;
    private FaceHelper faceHelper;
    private boolean selfCheckIn;
    private boolean scan;

    public RecognizeSecPresenter(RecognizeContract.View view, Context context, FaceHelper faceHelper) {
        this.view = view;
        idCardHelper = new IdCardHelper(context);
        this.faceHelper = faceHelper;
    }

    @Override
    public void start() {
        idCardHelper.open(new IdCardHelper.OnReadIdCard() {
            @Override
            public void onReadIdCard(final IDCard idCard) {
                if (!scan) return;
                if (selfCheckIn) {
                    final Pair<String, List<SignPerson>> pair = Api.signInfo(idCard.getName(),idCard.getIDCardNo());
                    final List<SignPerson> people = pair.second;
                    switch (pair.first) {
                        case "500":
                            view.showBackInfo("服务器错误", Constant.Color.COLOR_FAIL);
                            break;
                        case "600":
                            view.showBackInfo("网络错误", Constant.Color.COLOR_FAIL);
                            break;
                        case "no":
                            view.showBackInfo("系统未录入你的信息", Constant.Color.COLOR_WARNNING);
                            break;
                        case "md5":
                            SignPerson person = people.get(0);
                            String sex = "男";
                            if (TextUtils.isEmpty(person.getSex())) sex = "未知";
                            else if (person.getSex().equals("2")) sex = "女";
                            person.setSex(sex);
                            if (TextUtils.isEmpty(person.getSignTime())) {
                                view.showCardInfo(person.getName(),
                                        person.getSex().equals("2") ? "女" : "男",
                                        person.getWorkPlace(),
                                        person.getHotelName(),
                                        person.getRoomNumber(),
                                        idCard.getPhoto());
                                Bitmap bitmap = faceHelper.compareIdcard(idCard.getPhoto());
                                person.setDocumentNumber(idCard.getIDCardNo());
                                view.showCompareInfo(bitmap, idCard.getPhoto());
                                if (bitmap!=null) view.goCheckPage(person);
                            } else {
                                view.showBackInfo("重复签到", Constant.Color.COLOR_WARNNING);
                            }
                            break;
                        case "name":
                            Bitmap bitmap = faceHelper.compareIdcard(idCard.getPhoto());
                            view.showCompareInfo(bitmap, idCard.getPhoto());
                            if (bitmap!=null){
                                view.goOtherPage(OthersFragment.DO_SIGN_SELF,people,idCard.getIDCardNo(),idCard.getName());
                            }
                            break;
                    }
                }else {
                    view.showCardInfo(idCard.getName(), idCard.getSex(), "", "", "", idCard.getPhoto());
                    Bitmap bitmap = faceHelper.compareIdcard(idCard.getPhoto());
                    view.showCompareInfo(bitmap, idCard.getPhoto());
                    if (bitmap!=null){
                        view.goOtherPage(OthersFragment.DO_SIGN_OTHER,null,idCard.getIDCardNo(),idCard.getName());
                    }
                }
            }
        });
    }

    @Override
    public void pauseScan() {
        scan = false;
    }

    @Override
    public void resumeScan() {
        scan =true;
    }

    @Override
    public void release() {
        idCardHelper.close();
        faceHelper.release();
    }

    @Override
    public void setSelfCheckIn(boolean selfCheckIn) {
        this.selfCheckIn = selfCheckIn;
    }
}
